package com.lu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.reggie.dto.DishDto;
import com.lu.reggie.entity.Dish;
import com.lu.reggie.entity.DishFlavor;
import com.lu.reggie.exceptions.BusinessException;
import com.lu.reggie.mapper.DishMapper;
import com.lu.reggie.service.IDishFlavorService;
import com.lu.reggie.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.annotation.HttpConstraint;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜品管理 服务实现类
 * </p>
 *
 * @author 况山云
 * @since 2022-08-22
 */
@Slf4j
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {
    @Autowired
    private IDishFlavorService iDishFlavorService;
    @Autowired
    private IDishService iDishService;

    /**
     * 添加菜品,同时保存两张表的数据
     * @param dishDto
     */
    @Override
    //多张表操作需要开启事务确保
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //先保存dish表
        this.save(dishDto);
        //前端传过来的口味集合中不包含id，所以需要手动获取id
        Long dishDtoId = dishDto.getId();
        //lambdastream流给每个口味集合加上id
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.stream().map((item) -> {
            item.setDishId(dishDtoId);
            return item;
        }).collect(Collectors.toList());

        //口味信息保存到口味表
        iDishFlavorService.saveBatch(flavors);
    }

    /**
     * dish和flavor同时查询数据根据id
     * @param id
     * @return
     */
    @Override
    public DishDto findByIdWithFlavor(Long id) {
        //先查询dish
        Dish dish = this.getById(id);
        //使用dto
        DishDto dishDto = new DishDto();
        //把数据copy过去
        BeanUtils.copyProperties(dish, dishDto);
        //去查flavor,条件过滤器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //根据菜品的id查询
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        //查询
        List<DishFlavor> dishFlavors = iDishFlavorService.list(queryWrapper);
        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    /**
     * 修改菜品信息,包含口味
     * @param dishDto
     */
    @Override
    @HttpConstraint
    public void updateByIdWithFlavor(DishDto dishDto) {
        //先更新dish
        this.updateById(dishDto);
        //口味表的数据线删除后添加==更新
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        iDishFlavorService.remove(queryWrapper);

        //插入
        List<DishFlavor> dishFlavors = dishDto.getFlavors();
        dishFlavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        iDishFlavorService.saveBatch(dishFlavors);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ids != null, Dish::getId, ids);
        List<Dish> dishList = iDishService.list(queryWrapper);
        for (Dish dish : dishList) {
            Integer status = dish.getStatus();
            if (status == 0) {
                //停售就可以删除
                this.removeById(dish.getId());
            } else {
                throw new BusinessException("该菜品正在销售!无法删除!");
            }
        }
    }
}
